require'net/http'
require'json'
require'yaml'
require'base64'
module JavaEye
  AUTH_URL="http://api.javaeye.com/api/auth/verify"
  VIA="EyeShoes"
  LIST_URL="http://api.javaeye.com/api/twitters/list"
  REPLY_URL='http://api.javaeye.com/api/twitters/replies'
  ALL_URL='http://api.javaeye.com/api/twitters/all'
  CREATE_URL="http://api.javaeye.com/api/twitters/create"
  DELETE_URL="http://api.javaeye.com/api/twitters/destroy"
  SHOW_URL="http://api.javaeye.com/api/twitters/show"
  class User
    %W[name domain id logo].each do |attr|
      attr_accessor attr.to_sym
    end
    def initialize(hash)
      hash.each do |key,value|
        self.send("#{key}=",value)
      end
    end
  end
  class Twitter
    %W[user id created_at body name logo domain receiver reply_to_id via].each do |attr|
      attr_accessor attr.to_sym
    end
    def initialize(hash)
      hash.each do |key,value|
        if key=="user"
          self.send("#{key}=",User.new(value))
        else
        self.send("#{key}=",value)
        end
      end
    end
  end
  class Client
    attr_accessor:user
    attr_accessor:auth_code
    def initialize(username,password)
      @username=username
      @password=password
    end
    def auth
        resp=basic_get_request(AUTH_URL)
        case resp.code
        when "200"
          @auth_code="200"
          @user=User.new(JSON.parse(resp.body))
        when "400"
          @auth_code="400"
        when "401"
          @auth_code="401"
        end
    end
    def option2query(option)
       query=''
       option.each do |key,value|
        query<<(key.to_s+'='+value.to_s+'&')
      end
      query.chop
    end
    def get_request_uri(path,option)
      query=option2query(option)
      if query.empty?
        path
      else
        path+'?'+query
      end
    end
    def basic_get_request(url,option={})
      url=get_request_uri(url,option)
      url=URI.parse(url)
      Net::HTTP.start(url.host) do |http|
        req=Net::HTTP::Get.new(url.request_uri)
        req.basic_auth @username,@password
        resp=http.request(req)
        return resp
      end
    end
    def basic_post_request(url,data,option={})
      url=get_request_uri(url,option)
      url=URI.parse(url)
      Net::HTTP.start(url.host) do |http|
        req=Net::HTTP::Post.new(url.request_uri)
        req.basic_auth @username,@password
        req.set_form_data(data)
        resp=http.request(req)
        return resp
      end
    end
    def get_twitters(url,option)
      resp=basic_get_request(url,option)
      case resp.code
        when "200"
          @auth_code="200"
          twitters||=[]
          JSON.parse(resp.body).each do |twitter|
            twitters<<Twitter.new(twitter)
          end
          twitters
        when "400"
          @auth_code="400"
          nil
        when "401"
          @auth_code="401"
          nil
      end
    end
    def get_list(option={})
      get_twitters(LIST_URL,option)
    end
    def get_reply(option={})
      get_twitters(REPLY_URL,option)
    end
    def get_all(option={})
      get_twitters(ALL_URL,option)
    end
    def create(data)
      data['via']=VIA
      resp=basic_post_request(CREATE_URL,data)
      if resp.code=='200'
        true
      else
        false
      end
    end
    def delete(id)
      data={}
      data["id"]=id.to_s
      resp=basic_post_request(DELETE_URL,data)
      if resp.code=='200'
        true
      else
        false
      end
    end
    def show(id)
      data={}
      data["id"]=id.to_s
      resp=basic_post_reqest(SHOW_URL,data)
      if resp.code=='200'
        twitters=[]
        (JSON.parse resp.body).to_a.each do |twitter|
          twitters<<Twitter.new(twitter)
        end
        twitters
      else
        nil
      end
    end
  end
end
Shoes.app(:title=>"EyeShoes",:height=>140,:width=>300,:resizable=>false,:scroll=>false) do
  Login_bgcolor=rgb(244,255,255)
  Main_bg_color=rgb(255,255,210)
  Main_bg_url="http://lh3.ggpht.com/_EXkagovAOWI/ScAsH8R_Z6I/AAAAAAAAASc/9daGHGDLLs8/b.jpg"
  Defalt_head_url="http://hooopo.javaeye.com/upload/picture/pic/33145/c57d97f2-acad-3eea-ab7b-d116f1c77277.png"
  Reply_icon_url="http://hooopo.javaeye.com/upload/picture/pic/33143/3cb38525-45e2-3be0-907c-07acc0984d70.png"
  Flow_bg_color=rgb(255,215,0)..rgb(75,0,13)
  background Login_bgcolor
  main=self
  hash={:user=>"",:password=>""}
  if File.exists?("log.yaml")
    hash=YAML::load(File.open("log.yaml"))
    hash[:user]=Base64::decode64(hash[:user])
    hash[:password]=Base64::decode64(hash[:password])
  end
  stack(:scroll=>false) do
    flow(:margin_left=>25,:margin_top=>10) do
      para "UserName: ",:font=>'song'
      @user=edit_line(hash[:user],:width=>140)
    end
    flow(:margin_left=>25) do
       para "PassWord:  ",:font=>"song"
      @password=edit_line(hash[:password],:secret=>true,:width=>140)
    end
    flow(:margin_left=>50) do
      @check=check(:checked=>true)
      para "Don't forget it?",:font => "song"
    end
    flow do
      button "Submit",:margin_left=>25,:font=>"song" do
        client=JavaEye::Client.new(@user.text,@password.text)
        client.auth
        if client.auth_code=="200"
          if @check.checked?
            hash={:user=>Base64::encode64(@user.text),:password=>Base64::encode64(@password.text)}
            yaml=YAML::dump(hash)
            File.open("log.yaml","w") do |f|
              f.write yaml
            end
          end
          UserName=@user.text
          PassWord=@password.text
          window(:title=>"EyeShoes--"+client.user.name, :width => 284,:height=>650, :resizable => false) do
              @max_id="0"
              @flag=:list
              def show_twitter(option=:all)
                c=JavaEye::Client.new(UserName,PassWord)
                case option
                when :all
                  twitters=c.get_all
                when :list
                  twitters=c.get_list
                when :reply
                  twitters=c.get_reply
                end
                if twitters[0].id.to_i>@max_id.to_i
                  @max_id=twitters[0].id
                end
                @disply_stack.remove
                @navigate_flow.prepend do
                    @disply_stack=stack(:width=>284,:height=>500,:scroll=>true)  do
                          twitters[0,5].each do |t|
                                flow(:margin_right=>18) do
                                background(Flow_bg_color,:curve=>6,:margin=>5)
                                    stack(:width=>60) do
                                      if t.user.logo
                                        image("http://www.javaeye.com"+ t.user.logo.sub(/\./,'-thumb.'),:margin=>6,:click=>"http://#{t.user.domain}.javaeye.com")
                                      else
                                        image(Defalt_head_url,:margin=>6,:click=>"http://#{t.user.domain}.javaeye.com")
                                      end
                                      image(Reply_icon_url,:top=>7,:left=>7).click do
                                        @body.text="@#{t.user.name} "
                                        @reply_to_id=t.id
                                      end
                                    end
                                    stack(:width=>-60) do
                                        flow do
                                           para t.user.name + " says:",:stroke=>blue,:font=>"song",:size=>9
                                           para "via " + t.via.to_s,:size=>7,:stroke=>green,:margin_left=>10 
                                        end
                                        para t.body  ,:font=>"song",:size=>8,:stroke=>darkblue
                                        para t.created_at[0,19],:font=>"smallfont",:size=>7,:stroke=>"#DFC",:margin_left=>100
                                     end
                               end
                          end
                    end
                end
              end
              background Main_bg_color
              client=JavaEye::Client.new(UserName,PassWord)
              client.auth
              if client.auth_code=="200"
                stack do
                @disply_stack=stack do
                end
                @navigate_flow=flow do
                  @c1=radio(:checked=>true,:group=>:radios) do
                    @flag=:list
                    show_twitter(:list) 
                  end
                  para "我的订阅",:font=>"song",:size=>8
                  @c2=radio(:group=>:radios) do
                    @flag=:reply
                    show_twitter(:reply)
                  end
                  para "我的回复",:font=>"song",:size=>8
                  @c3=radio(:group=>:radios)do 
                    @flag=:all
                    show_twitter(:all)
                  end
                  para "全站闲聊",:font=>"song",:size=>8
                end
                @say_stack=stack do
                  @body=edit_box(:width=>280,:height=>100)
                  @body.text="What are you doing?"
                end
                @submit=button("SendMessage",:margin_left=>2) do
                  client.create(:body=>@body.text,:reply_to_id=>@reply_to_id)
                  @reply_to_id=nil
                  @body.text=""
                  show_twitter(:list)
                end
                end
                 show_twitter(:list)
                 every(90) do
                   case @flag
                   when :list
                     show_twitter(:list)
                   when :all
                     show_twitter(:all)
                   when :reply
                     show_twitter(:reply)
                   end
                 end
              else
                alert "Login Fail!"
              end
          end
          main.close
        end
      end#end of button
      button "Reset",:font=>"song" do
      @user.text=''
      @password.text=''
      @user.focus
    end
  end#end of flow
 end#end of stack
end#end of app